package client.ui.controllers;

/*
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 * RegisterScreen.java
 *
 * Steven Louie Apr. 8, 2012
 */
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.Label;
import de.lessvoid.nifty.controls.TextField;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

public class RegisterScreen implements ScreenController {

    private Nifty nifty;

    @Override
    public void bind(Nifty nifty, Screen screen) {
        this.nifty = nifty;
    }

    @Override
    public void onStartScreen() {
    }

    @Override
    public void onEndScreen() {
    }

    public void register() {
        Label info = nifty.getCurrentScreen().findNiftyControl("info", Label.class);
        Label info2 = nifty.getCurrentScreen().findNiftyControl("info2", Label.class);
        // Clear the info labels
        info.setText("");
        info2.setText("");

        String first = nifty.getCurrentScreen().findNiftyControl("firstname",
                TextField.class).getText();
        String last = nifty.getCurrentScreen().findNiftyControl("lastname",
                TextField.class).getText();
        String user = nifty.getCurrentScreen().findNiftyControl("username",
                TextField.class).getText();
        String pass = nifty.getCurrentScreen().findNiftyControl("password",
                TextField.class).getText();
        String passC = nifty.getCurrentScreen().findNiftyControl("passwordC",
                TextField.class).getText();
        String email = nifty.getCurrentScreen().findNiftyControl("email",
                TextField.class).getText();
        if (first == null || first.equals("") || last == null
                || last == null || last.equals("") || last == null) {
            info.setText("Invalid Name");
            return;
        }
        if (user == null || user.equals("")) {
            info.setText("Invalid username");
            return;
        }
        if (pass == null || pass.equals("") || pass.length() < 6) {
            info.setText("Password is too short");
            return;
        }
        if (!pass.equals(passC)) {
            info.setText("Passwords do not match");
            return;
        }
        if (!email.contains("@")) {
            info2.setText("Invalid e-mail");
            return;
        }

        // Send to server
    }

    public void gotoLogin() {
        clearData();
        nifty.gotoScreen("start");
    }

    public void clearData() {
        nifty.getCurrentScreen().findNiftyControl("firstname",
                TextField.class).setText("");
        nifty.getCurrentScreen().findNiftyControl("lastname",
                TextField.class).setText("");
        nifty.getCurrentScreen().findNiftyControl("username",
                TextField.class).setText("");
        nifty.getCurrentScreen().findNiftyControl("password",
                TextField.class).setText("");
        nifty.getCurrentScreen().findNiftyControl("passwordC",
                TextField.class).setText("");
        nifty.getCurrentScreen().findNiftyControl("email",
                TextField.class).setText("");
        nifty.getCurrentScreen().findNiftyControl("info",
                Label.class).setText("");
        nifty.getCurrentScreen().findNiftyControl("info2",
                Label.class).setText("");
    }
}
